<?php
	//邀请接口 invite.php
	//create by L
	//http://42.121.57.193:7788/platform/invite.php?phone=15507558558&invite_phone=15507558551&type=2
	//type 1-注册 2-充值

	//初始化--
	include_once "init.php";

	$phone        	= $_GET["phone"];
	$invite_phone 	= $_GET["invite_phone"];
	$type 			= $_GET["type"];

	if('' == $invite_phone )
	{
		die("-1|被邀请手机号码不能为空");
	}
	if (strlen($invite_phone) != 11 || false==is_numeric($invite_phone) || ord($invite_phone)!=49)
	{
		die("-1|手机号码必须是11位数字，并且以1开头");
	}
	if('' == $phone )
	{
		die("-1|手机号码不可以为空");
	}
	if (strlen($phone) != 11 || false==is_numeric($phone) || ord($phone)!=49)
	{
		die("-1|手机号码必须是11位数字，并且以1开头");
	}
	if('' == $type )
	{
		die("-1|邀请类型不能为空");
	}
	if( $type > '2' || $type < '0')
	{
		die("-1|邀请类型错误");
	}

	$con = db_connect(DB_NAME);

	//检测发起邀请的号码是否存在
	$result = mysql_query("SELECT phone from user where phone='$phone'") or die("-1|查询数据库失败");
	$row = mysql_fetch_row($result);

	if (empty($row))
	{
		mysql_close($con);
		die("-1|发起邀请手机号码不存在");
	}
	//检测被邀请号码是否存在--查询邀请表
	$result = mysql_query("SELECT phone from invite where phone='$invite_phone'") or die("-1|查询数据库失败");
	$row = mysql_fetch_row($result);

	if (!empty($row))
	{
		mysql_close($con);
		die("1|被邀请手机号码已经邀请过");
	}

	//检测被邀请号码是否注册过--查询USER表
	$result = mysql_query("SELECT phone from user where phone='$invite_phone'") or die("-1|查询数据库失败");
	$row = mysql_fetch_row($result);

	if (!empty($row))
	{
		mysql_close($con);
		die("2|被邀请手机号码已经注册过");
	}

	$invite_time = date('Y-m-d H:i:s');
	$flag = 0;

	//邀请记录录入
	mysql_query("LOCK TABLES invite WRITE");
	$result = mysql_query("INSERT INTO invite (phone,invite_phone,invite_type,invite_time,flag) VALUES ('".$phone."','".$invite_phone."','".$type."','".$invite_time."',".$flag.")");
	mysql_query("UNLOCK TABLES");

	if(FALSE==$result)
	{
		mysql_close($con);
		die("-1|邀请记录失败");
	}

	mysql_close($con);

	echo '0|';

?>
